<?php
class mpUser extends SilkUser {
	public function __construct(){
		parent::__construct();
	}
    
	public function setup() {
		parent::setup();
		$this->create_has_and_belongs_to_many_association("friends", "mpUser", "friends", "friend_id", "user_id");
		$this->create_has_many_association("portfolios", "Portfolio", "user_id");
		$this->create_has_many_association("activity", "UserActivity", "user_id", array("order" => "create_date DESC"));
		$this->create_has_many_association("recentActivity", "UserActivity", "user_id", array("order" => "create_date DESC", "limit" => array(1,5)));
		$this->create_has_many_association("recentFriendsActivity", "UserActivity", "user_id", array("join" => "INNER JOIN"));
	}
	
	public function get_portfolios() {
		$portfolios = orm("Portfolio")->find_all(array(	"conditions" => array("user_id = " . $this->id)));
		$this->portfolios = $portfolios;
		return $portfolios;
	}
	
	public function is_friend($friend_id) {
//		$friends = $this->friends;
		$friends = db()->Execute("SELECT friend_id FROM silk_friends WHERE user_id=$friend_id");
		if( !empty( $friends )) {
			echo "<pre>"; var_dump($friends); echo "</pre>"; return true;
			if( in_array($friend_id, $friends ) ) {
				return true;
			}
		}
		return false;
	}
	
	public function get_friends() {
		$friends = db()->Execute("SELECT friend_id FROM silk_friends WHERE user_id=$this->id");
//		echo "<pre>Friends"; var_dump($friends); echo "</pre>";
		$results = array();
		foreach( $friends as $friend ) {
			foreach( $friend as $friend_id ) {
//				echo "<pre>"; var_dump($friend_id); echo "</pre>";
				$results[] = $friend_id;
			}
		}
		return $results;
	}
}
?>